<?php
namespace Common\Model;
use Common\Model\AiModel;
class PayLogModel extends AiModel {
    #设置交易单支付状态
    function setPay($order_sn) {
        $pay_log = D('PayLog')->where(array('order_sn'=>$order_sn))->find();
        if ($pay_log['pay_time']>0) return true;

        $data = array('pay_time' => ADD_TIME);
        $result = $this->where("id=".$pay_log['id'])->data($data)->save();
        if($result){

        }
        switch($pay_log['pay_type']){
            case 'hypertension':
                D('Order')->setPay($pay_log['id']);
                break;
        }

        return $result;
    }

    #将订单表插入paylog表中，生成pay_log_id
    public function addPayLog($payment_id,$bank,$pay_type='gene',$money,$user_id=0){
        if($user_id==0) $user_id = intval(UID);
        $app_type='pregnant';
        if (strpos($_COOKIE['ext_params'], 'jojohealth')) {

            $app_type='health';

        }

        $data = array(
            'order_sn'   => get_order_sn(),
            'pay_type'   => $pay_type,
            'app_type'   => $app_type,
            'payment_id' => $payment_id,
            'bank' 		 => $bank,
            'money'      => $money,
            'user_id'    => $user_id,
            'pay_time'   => 0,
            'add_time'   => ADD_TIME,
            'add_ip'     => ADD_IP
        );
        return $this->add($data);
    }

    /**
     * @param int $pay_log_id
     * 更新paylog信息
     */
    public function updateInfo($pay_log_id=0){

        $info = $this->find($pay_log_id);
        $order_sn = get_order_sn();
        $this->where("id={$pay_log_id}")->data(array('order_sn'=>$order_sn))->limit(1)->save();
        $info['order_sn'] = $order_sn;

        return $info;
    }


    public function getOrderSn($pay_log_id=0){
        return $this->where("id={$pay_log_id}")->getField('order_sn');
    }
}